package reviewAlgorithms.dynamicProgramming;

public class IntegerBreak {
    public static void main(String[] args) {
        System.out.println(myIntegerBreak(10));
    }

    private static int myIntegerBreak(int n) {
        if (n < 2) {
            return 0;
        }
        int[] dp = new int[n + 1];
        dp[2] = 1;
        for (int i = 3; i <= n; i++) {
            int res = 0;
            for (int j = 1; j < i - 1; j++) {
                res = Math.max(Math.max(j*dp[i - j],j*(i - j)),res);
            }
            dp[i] = res;
        }
        return dp[n];
    }

    /**
     * 数学公式推导
     * @param n
     * @return
     */
    public static int integerBreak(int n) {
        if(n <= 3) return n - 1;
        int a = n / 3, b = n % 3;
        if(b == 0) return (int)Math.pow(3, a);
        if(b == 1) return (int)Math.pow(3, a - 1) * 4;
        return (int)Math.pow(3, a) * 2;
    }
}
